﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmallERP.MODEL.PBModel;
using SmallERP.Bll.PinBan;
using Smallerp.Contracts.DataContracts;
using Smallerp.Contracts;

namespace SmallERP.PinBan.PB.Froms
{
    public partial class ModifyOrder : Form
    {
        public OrderInfo_PBDCT ModifyOrderInfo { get; set; }


        public ModifyOrder(List<PaperFaceDCT> inlistface, List<PaperTypeDCT> inListPapertype, IOperateDB InoperateDB)
        {
            InitializeComponent();
            Listface = inlistface;
            ListPapertype = inListPapertype;
            _operateDB = InoperateDB;
        }
        IOperateDB _operateDB;
        List<PaperFaceDCT> Listface; List<PaperTypeDCT> ListPapertype;
        private void ModifyOrder_Load(object sender, EventArgs e)
        {
            if (ModifyOrderInfo != null)
            {
                txtOrderName.Text = ModifyOrderInfo.OrderName;
                txtOrderPath.Text = ModifyOrderInfo.order_Base_Info.OrderPath;
                txtPoroducNum.Value = ModifyOrderInfo.ProductNum;
                comboBoxPriority.Text = ModifyOrderInfo.order_Base_Info.Priority == 0 ? "普通" : "加急";
                txtRemark.Text = ModifyOrderInfo.order_Base_Info.Remark;
                cmbPaperFace.Text = ModifyOrderInfo.PhotoList.FirstOrDefault().PaperFaceName;
                cmbPpaerType.Text = ModifyOrderInfo.PhotoList.FirstOrDefault().PaperTypeName;
            }

        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (btnModify.Text == "修改")
            {
                btnModify.Text = "保存";
                btnCancel.Enabled = true;
                this.groupBox1.Enabled = true;
                //加载纸型纸面列表
                cmbPaperFace.DataSource = Listface;
                cmbPaperFace.DisplayMember = "PaperFaceName";
                cmbPaperFace.ValueMember = "PaperFaceId";

                cmbPpaerType.DataSource = ListPapertype;
                cmbPpaerType.DisplayMember = "PaperTypeName";
                cmbPpaerType.ValueMember = "PaperTypeId";

                cmbPaperFace.SelectedValue = ModifyOrderInfo.order_Base_Info.PaperFaceId;
                cmbPpaerType.SelectedValue = ModifyOrderInfo.order_Base_Info.PaperTypeId;
                //cmbPaperFace.Text = ModifyOrderInfo.PhotoList.FirstOrDefault().PaperFaceName;
                //cmbPpaerType.Text = ModifyOrderInfo.PhotoList.FirstOrDefault().PaperTypeName;
            }
            else//保存
            {
                btnModify.Text = "修改";
               
                btnModify.Enabled = false;
                dxErrorProvider1.SetError(null, "");

                if (string.IsNullOrEmpty(txtOrderName.Text))
                {
                    dxErrorProvider1.SetError(txtOrderName, "不能为空");
                    return;
                }
                if (txtPoroducNum.Value < 0)
                {
                    dxErrorProvider1.SetError(txtPoroducNum, "份数不能小于1");
                    return;
                }
                //if (txtPriority.Value < 0)
                //{
                //    dxErrorProvider1.SetError(txtPriority, "加急级别不能为负数");
                //    return;
                //}
                if ((int)cmbPaperFace.SelectedValue < 0)
                {
                    dxErrorProvider1.SetError(cmbPaperFace, "请选择纸面");
                    return;
                }
                if ((int)cmbPpaerType.SelectedValue < 0)
                {
                    dxErrorProvider1.SetError(cmbPpaerType, "请选择纸型");
                    return;
                }

                ModifyOrderInfo.order_Base_Info.OrderName = txtOrderName.Text;
                ModifyOrderInfo.order_Base_Info.ProductNum = (int)txtPoroducNum.Value;
                ModifyOrderInfo.order_Base_Info.Priority = comboBoxPriority.SelectedIndex;
                ModifyOrderInfo.order_Base_Info.Remark = txtRemark.Text;
                ModifyOrderInfo.order_Base_Info.PaperFaceId = (int)cmbPaperFace.SelectedValue;
                ModifyOrderInfo.order_Base_Info.PaperTypeId = (int)cmbPpaerType.SelectedValue;
                OrderBLL _bll = new OrderBLL("", 0, _operateDB);
                int result = _bll.ModifyOrder(ModifyOrderInfo);
                if (result==1)
                {          
                    btnModify.Text = "修改";
                    groupBox1.Enabled = false;
                    MessageBox.Show("保存成功");                    //成功
                }

                btnCancel.Enabled = false;
                btnModify.Enabled = true;
            }

            this.Cursor = Cursors.Default;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnModify.Text = "修改";
            groupBox1.Enabled = false;
            txtOrderName.Text = ModifyOrderInfo.OrderName;
            txtOrderPath.Text = ModifyOrderInfo.order_Base_Info.OrderPath;
            txtPoroducNum.Value = ModifyOrderInfo.ProductNum;
            comboBoxPriority.Text = ModifyOrderInfo.order_Base_Info.Priority == 0 ? "普通" : "加急";
            
            txtRemark.Text = ModifyOrderInfo.order_Base_Info.Remark;
            cmbPaperFace.Text = ModifyOrderInfo.PhotoList.FirstOrDefault().PaperFaceName;
            cmbPpaerType.Text = ModifyOrderInfo.PhotoList.FirstOrDefault().PaperTypeName;
            btnCancel.Enabled = false;
        }

        
    }
}
